package com.sgcc.pda.gis;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import com.sgcc.pda.hzaidl.entity.GisCallback;
import com.sgcc.pda.hzaidl.impl.GisUtil;
import com.sgcc.platforminterface.PlatfInterfManager;


/**
 * @创建者：suven on 2020/3/24 11:48
 * @描述：Gis相关功能
 * @版本：1.0
 * @修改人：
 * @修改时间：
 * @修改说明：
 * @修改版本：
 */
public class GisManager {

    private static final String PACKAGE_NAME = "com.sgcc.pda.gis";
    private static final String CLASS_NAME = "com.sgcc.pda.gis.AIDLService";


    private static GisManager singleTon;

    private Context mAppContext;
    /**
     * 远程服务连接对象
     */
    private ServiceConnection mRemoteConn;
    /**
     * 远程服务
     */
    private IGisInterface mRemoteService;

    /**
     * 远程服务绑定监听
     */
    interface RemoteConnectCallback {
        /**
         * 绑定成功
         */
        void onConnect();
    }


    public static synchronized GisManager getInstance() {
        if (null == singleTon) {
            singleTon = new GisManager();
        }
        return singleTon;
    }

    public GisManager init(Context context) {
        mAppContext = context.getApplicationContext();
        return this;
    }

    /**
     * 绑定远程服务
     *
     * @param connectCallback 远程服务绑定监听
     */
    public void bindRemoteService(final RemoteConnectCallback connectCallback) {
        if (null == mRemoteService) {
            if (null == mRemoteConn) {
                mRemoteConn = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {

                        mRemoteService = IGisInterface.Stub.asInterface(service);
                        if (null == mRemoteService) {
                            unBindRemoteService();
                        } else if (null != connectCallback) {
                            connectCallback.onConnect();
                        }
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        restartRemoteService(null);
                    }
                };
            }
            mAppContext.bindService(new Intent(CLASS_NAME).setPackage(PACKAGE_NAME), mRemoteConn, Context.BIND_AUTO_CREATE);
        } else if (mRemoteService.asBinder().isBinderAlive()) {
            if (null != connectCallback) {
                connectCallback.onConnect();
            }
        } else {
            restartRemoteService(connectCallback);
        }
    }

    /**
     * 重启远程服务：解绑再绑定
     *
     * @param connectCallback 远程服务绑定监听
     */
    private void restartRemoteService(RemoteConnectCallback connectCallback) {
        unBindRemoteService();
        bindRemoteService(connectCallback);
    }

    /**
     * 解绑远程服务
     */
    public void unBindRemoteService() {
        if (null != mRemoteConn) {
            try {
                mAppContext.unbindService(mRemoteConn);
                mRemoteService = null;
                mRemoteConn = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 远程服务已绑定且运行正常
     *
     * @return 是true否则false
     */
    private boolean isRemoteServiceAvailable() {
        return null != mRemoteConn && null != mRemoteService && mRemoteService.asBinder().isBinderAlive();
    }

    /**
     * 获取一次经纬度
     *
     * @param callback 回调
     */
    public void locateOnce(final LocationCallback callback)  {
        // TODO: 2020/4/26 添加极速版方法
        if (PlatfInterfManager.getInstance().isSpeed()) {
            GisUtil.requestGisData("gis_getLocation", new GisCallback() {
                @Override
                public void onReceiverData(LocationResult locationResult) throws RemoteException {
                    if (locationResult != null)
                        callback.onResult(locationResult.getLat(), locationResult.getLon());
                }
            });
        } else {
            if (isRemoteServiceAvailable()) {
                try {
                    mRemoteService.getLocation(callback);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            } else {
                bindRemoteService(new RemoteConnectCallback() {
                    @Override
                    public void onConnect() {
                        try {
                            mRemoteService.getLocation(callback);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }

    /**
     * 获取最近一次经纬度
     */
    public LocationResult getLastValidLocation() {
        if (PlatfInterfManager.getInstance().isSpeed()) {
            try {
                return GisUtil.requestGisData("gis_getLastValidLocation");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        } else {
            if (isRemoteServiceAvailable()) {
                try {
                    return mRemoteService.getLastValidLocation();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
}
